package com.test.springboot.mybatis.service.impl;

import com.test.springboot.mybatis.dao.mapper.PayRecordMapper;
import com.test.springboot.mybatis.entity.PayRecord;
import com.test.springboot.mybatis.entity.PayRecordExample;
import com.test.springboot.mybatis.entity.enums.EnumPayRecordStatus;
import com.test.springboot.mybatis.entity.enums.EnumRecordType;
import com.test.springboot.mybatis.service.PayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author cxb
 * @date 2018年8月8日
 */
@Slf4j
@Service
public class PayRecordServiceImpl implements PayRecordService {

    @Autowired
    private PayRecordMapper payRecordRecordMapper;

    @SuppressWarnings("unchecked")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(PayRecord record) {
        if (null != record) {
            int res = payRecordRecordMapper.insert(record);
            return 1 == res ? record.getId() : 0;
        }
        return 0;
    }

    @Override
    public PayRecord getById(int id) {
        if (id > 0) {
            return payRecordRecordMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public PayRecord getIncomplateByTableKeyAndPayer(String tableKey, String payer) {
        if (null != tableKey && !"".equals(tableKey = tableKey.trim()) && null != payer
                && !"".equals(payer = payer.trim())) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria().andPayerEqualTo(payer).andTableKeyEqualTo(tableKey)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andStatusEqualTo(EnumPayRecordStatus.INCOMPLATE.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(int id) {
        if (id > 0) {
            return payRecordRecordMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(PayRecord record) {
        if (null != record && record.getId() > 0) {
            return payRecordRecordMapper.updateByPrimaryKey(record);
        }
        return 0;
    }

    @Override
    public PayRecord getByTableKeyAndBillNo(String tableKey, String billNo) {
        if (null != tableKey && null != billNo) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria()
                    .andTableKeyEqualTo(tableKey)
                    .andBillNoEqualTo(billNo)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andStatusEqualTo(EnumPayRecordStatus.COMPLATE.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public PayRecord getByTableKeyAndBillId(String tableKey, String billId) {
        if (null != tableKey && null != billId) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria()
                    .andTableKeyEqualTo(tableKey)
                    .andBillIdEqualTo(billId)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andStatusEqualTo(EnumPayRecordStatus.COMPLATE.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public PayRecord getComplateByTableKeyAndPayer(String tableKey, String payer) {
        if (null != tableKey && !"".equals(tableKey = tableKey.trim()) && null != payer
                && !"".equals(payer = payer.trim())) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria().andPayerEqualTo(payer).andTableKeyEqualTo(tableKey)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andStatusEqualTo(EnumPayRecordStatus.COMPLATE.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public PayRecord getLastRecordByTableKeyAndPayer(String tableKey, String payer) {
        if (null != tableKey && !"".equals(tableKey = tableKey.trim()) && null != payer
                && !"".equals(payer = payer.trim())) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria().andPayerEqualTo(payer).andTableKeyEqualTo(tableKey)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public PayRecord getByBillId(String billId) {
        if (null != billId && !"".equals(billId = billId.trim())) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria()
                    .andBillIdEqualTo(billId)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public PayRecord getIncomplateBillNoAndPayer(String billNo, String payer) {
        if (null != billNo && !"".equals(billNo = billNo.trim()) && null != payer
                && !"".equals(payer = payer.trim())) {
            PayRecordExample example = new PayRecordExample();
            example.createCriteria().andPayerEqualTo(payer).andBillNoEqualTo(billNo)
                    .andRecordTypeEqualTo(EnumRecordType.PAY.getCode())
                    .andStatusEqualTo(EnumPayRecordStatus.INCOMPLATE.getCode())
                    .andCreateTimeGreaterThanOrEqualTo(
                            new Date(System.currentTimeMillis() - (1000L * 60 * 60)));
            example.setOrderByClause(" id DESC");

            List<PayRecord> list = payRecordRecordMapper.selectByExample(example);
            if (null != list && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

}
